import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'
import accountAdminApiConfig from '@/core/account/generated/apiconfig/accountAdminApiConfig'

const IamAccountPassportList = () => {
	const entityName = '账户通过证'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = accountAdminApiConfig.iamAccountPassport
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'iamAppId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'accountId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'passportType',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'passport',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'passport2',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'passport3',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'createTime',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'updateTime',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'rowVersion',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: { apiConfig: modelApiConfig.add },
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				id: '${id}',
				iamAppId: '${iamAppId}',
				accountId: '${accountId}',
				passportType: '${passportType}',
				passport: '${passport}',
				passport2: '${passport2}',
				passport3: '${passport3}',
			},
		} as DefaultCrudTableServerApi,
		debug: false,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				type: 'input-text',
				name: 'iamAppId',
				label: '账户应用id',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accountId',
				label: '账户id',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'passportType',
				label: '通行证类型',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'passport',
				label: '通行证第1部分',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'passport2',
				label: '通行证第2部分',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'passport3',
				label: '通行证第3部分',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'id',
				searchable: true,
			},
			{
				name: 'iamAppId',
				label: '账户应用id',
				searchable: true,
			},
			{
				name: 'accountId',
				label: '账户id',
				searchable: true,
			},
			{
				name: 'passportType',
				label: '通行证类型',
				searchable: true,
			},
			{
				name: 'passport',
				label: '通行证第1部分',
				searchable: true,
			},
			{
				name: 'passport2',
				label: '通行证第2部分',
				searchable: true,
			},
			{
				name: 'passport3',
				label: '通行证第3部分',
				searchable: true,
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default IamAccountPassportList
